package com.app.view.component.common 
{
	import flash.display.MovieClip;
	import flash.text.TextField;
	
	import com.greensock.TweenLite;
	
	import com.peachcastle.core.Display;
	
	import com.peachcastle.tools.DToolTip;
	
	import com.app.model.data.UserData;
	import com.app.model.data.LevelData;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class UserInfo extends Display
	{
		private var _levelIcon:MovieClip = null;
		
		private var _txtChips:TextField = null;
		private var _txtExp:TextField = null;
		
		private var _curExp:MovieClip = null;
		private var _totalExp:MovieClip = null;
		
		private var _upgradeLevel:MovieClip = null;
		
		public function UserInfo(ui:MovieClip) 
		{
			super(ui);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_levelIcon = assest["level"]["mc_level_icon"];
			
			_txtChips = assest["chips"]["txt_chips"];
			
			_txtExp = assest["level"]["txt_exp"];
			_curExp = assest["level"]["mc_cur_exp"];
			_totalExp = assest["level"]["mc_total_exp"];
			
			_upgradeLevel = assest["level"]["mc_upgrade_level"];
			_upgradeLevel.alpha = 0;
			
			DToolTip.register(assest["chips"], "籌碼", "middle");
			DToolTip.register(assest["level"], "經驗/等級", "bottom");
		}
		
		public function update(data:UserData, isNewLevel:Boolean = false, levelData:LevelData = null, useAnimationInExp:Boolean = false):void
		{
			if (data) _txtChips.text = String(data.chips);
			
			if (isNewLevel) onUpgradeLevelShow();
			
			if (levelData)
			{
				_levelIcon['txt_level'].text = String(levelData.sublevel);
				_levelIcon.gotoAndStop(levelData.star);

				_txtExp.text = String(data.exp) + " / " + String(levelData.totalExp);
				
				if (useAnimationInExp)
				{
					TweenLite.to(_curExp, .5, { width: (data.exp / levelData.totalExp) * _totalExp.width } );
				}else
				{
					_curExp.width = (data.exp / levelData.totalExp) * _totalExp.width;
				}
			}
		}
		
		public function onUpgradeLevelShow():void
		{
			TweenLite.to(_upgradeLevel, .7, { alpha:1} );
			
			TweenLite.delayedCall(.7, onUpgradeLevelHide);
		}
		
		private function onUpgradeLevelHide():void
		{
			TweenLite.to(_upgradeLevel, 1.5, { alpha:0} );
		}
	}

}